/* Copyright (C) 2004 The Trustees of Indiana University. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 * 
 * 2) All redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 * 
 * 3) Any documentation included with all redistributions must include the
 * following acknowledgement:
 * 
 * "This product includes software developed by the Indiana University Extreme!
 * Lab. For further information please visit http://www.extreme.indiana.edu/"
 * 
 * Alternatively, this acknowledgment may appear in the software itself, and
 * wherever such third-party acknowledgments normally appear.
 * 
 * 4) The name "Indiana University" or "Indiana University Extreme! Lab" shall
 * not be used to endorse or promote products derived from this software without
 * prior written permission from Indiana University. For written permission,
 * please contact http://www.extreme.indiana.edu/.
 * 
 * 5) Products derived from this software may not use "Indiana University" name
 * nor may "Indiana University" appear in their name, without prior written
 * permission of the Indiana University.
 * 
 * Indiana University provides no reassurances that the source code provided
 * does not infringe the patent or any other intellectual property rights of any
 * other entity. Indiana University disclaims any liability to any recipient for
 * claims brought by any other entity based on infringement of intellectual
 * property rights or otherwise.
 * 
 * LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO
 * WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 * NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF
 * INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS.
 * INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS",
 * "VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.
 * LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR
 * ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION
 * GENERATED USING SOFTWARE.
 */

package edu.indiana.cs.b534.torrent.impl;

import edu.indiana.cs.b534.torrent.context.TorrentContext;

import java.util.List;
import java.util.Map;

/**
 * @author : Eran Chinthaka (echintha@cs.indiana.edu)
 * @Date : Mar 25, 2007
 */
public class ChokingPolicyManager {

    private TorrentContext torrentContext;
    private InfoCollector infoCollector;

    public ChokingPolicyManager(TorrentContext torrentContext) {
        this.torrentContext = torrentContext;
        infoCollector = torrentContext.getInfoCollector();
    }

    public boolean shouldIChoke(PeerInstance peerInstance) {

        int chokingPolicy = torrentContext.getChokingPolicy();

        switch (chokingPolicy) {
            case Constants.CHOKING_POLICY_ALLOW_ALL:
                return true;
            case Constants.CHOKING_POLICY_PEERS_HAVING_MOST_INTERESTED_PIECES:
                return handleChokingPolicyPeersHavingMostInterestedPieces(peerInstance);
            case Constants.CHOKING_POLICY_SHARE_RATIO:
                return handleChokingPolicyShareRatio(peerInstance);
        }

        return true;
    }

    private boolean handleChokingPolicyShareRatio(PeerInstance peerInstance) {

        String peerId = peerInstance.getID();
        System.out.println("peerId = " + peerId);

        int downloadCount = 0;
        int uploadedCount = 0;
        double shareRatio = 0;

        // get the number of pieces downloaded
        Map<String, List<Integer>> downloadInformation = infoCollector.getDownloadInformation();
        List<Integer> pieceList = downloadInformation.get(peerId);
        if (pieceList != null) downloadCount = pieceList.size();

        // get the amount pieces uploaded
        Map<String, List<Integer>> uploadInformation = infoCollector.getUploadInformation();
        pieceList = uploadInformation.get(peerId);
        if (pieceList != null) uploadedCount = pieceList.size();

        System.out.println("uploadedCount = " + uploadedCount);
        System.out.println("downloadCount = " + downloadCount);

        // requesting peers share ratio, w.r.t. me is data uploaded by me / data downloaded by me
        // take the ratio and compare with the required ratio
        if (downloadCount > 0) {
            shareRatio = uploadedCount / downloadCount;
            System.out.println("shareRatio = " + shareRatio);
            return shareRatio < torrentContext.getShareRatio();
        } else {
            // I haven't downloaded from this guy. Have I uploaded to him? Let's give him a chance
            // up to two parts
            System.out.println("uploadedCount = " + uploadedCount);
            return uploadedCount > 2;
        }
    }

    private boolean handleChokingPolicyPeersHavingMostInterestedPieces(PeerInstance peerInstance) {
        return false;
    }
}
